Repository navigation
Make ending commands and waiting callers sleep instead of spinning - #193
Merged
Merged
Conversation
Callers that start a command or change the directory looped on cleanup_counter, a command that ends looped on pthread_mutex_trylock(&pid_mtx), and a command that ends in the middle of a pipeline looped on current_command_root_thread with an fflush each time. The counter now changes under a mutex and the callers sleep on a condition variable. The trylock loop is a pthread_mutex_lock. The pipeline wait sleeps on a condition that a command signals when it clears current_command_root_thread, and wakes every 50 ms to see whether the command was cancelled. tests/cleanup-wait.m measures the CPU of a pipeline whose middle command waits for the first one. ios_cleanupWait disables cancellation while it waits, so a command cancelled there does not keep cleanup_mutex. tests/cleanup-cancel.m cancels a thread blocked in chdir while a command ends.
demrom
marked this pull request as ready for review
October 24, 2026 04:22
Owner
|
Thanks a lot for that one as well. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three waits spun on a variable and kept the CPU busy, competing with the commands that would end them:
ios_nextAvailablePid,chdir,fchdirandfchdir_nolocklooped oncleanup_counter > 0until no command was ending.cleanup_functionlooped onpthread_mutex_trylock(&pid_mtx)while a caller was creating a pid.current_command_root_thread, callingfflusheach time, until the first command of the pipeline had ended.The counter now changes under a mutex and waiters sleep on a condition variable (
ios_cleanupBegin,ios_cleanupEnd,ios_cleanupWaitinlibc_replacement.c). Thetrylockloop becomes a lock and unlock. The pipeline wait sleeps on a condition signalled whencurrent_command_root_threadis cleared, and wakes every 50 ms to check whether the command was cancelled, since cancellation has no condition to signal.ios_cleanupWaitdisables cancellation while it waits:pthread_cond_waitis a cancellation point, and a command cancelled there (Ctrl-C in a pipeline,ios_kill) would wake holdingcleanup_mutexand block on it again incleanup_function, so every laterchdir,ios_forkand command end would hang. The cancel arrives at the next cancellation point instead.cleanup_counteris nowstatic, since nothing outsidelibc_replacement.cuses it any more.Test
tests/cleanup-wait.mrunshold | mid | skipwithjoinMainThread = false.midends after 300 ms whilehold(2 s) is still the root of the pipeline, and the test measures the process CPU time over 3 seconds. On the iOS 27 Simulator, master (fa2af59) fails 4 of 4 runs withFAIL, cpu=1.68s in 3 s, the ending command spun(1.68 s to 1.71 s); with this change 10 of 10 runs printcpu=0.01s in 3 s PASS.tests/cleanup-cancel.mpretends a command is ending (ios_cleanupBegin), cancels a command blocked inchdir, ends the pretend command and runs one more command. Without the cancellation guard it printsFAIL, no end after 20 sand exits 124 (5 of 5 runs); with it, 10 of 10 runs printPASS.Merged with the other four changes of the series,
concurrent-fork,concurrent-getopt,getopt-legacyandpid-admissionpass 5 of 5 runs each. On an iPhone, in a-Shell mini 2.2.3 (592) built from a-shell master with that mergedios_system,sleep 2 | sleep 1 | truefrom dash returned in 2 of 2 runs, after 3 s bydate +%s(whole seconds).This applies to master on its own. Once #192 and the changes stacked on it land, I rerun the merged tests on master.